<template>
  <el-dialog :title="!dataForm.gamePlayerId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="游戏昵称" prop="nicename">
            <el-input v-model="dataForm.nicename" placeholder="请输入玩家游戏昵称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分数" prop="score">
            <el-input v-model="dataForm.score" placeholder="请输入玩家排位分数"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="qq账号" prop="qqAccount">
            <el-input v-model="dataForm.qqAccount" placeholder="请输入玩家qq账号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          gamePlayerId: 0,
          nicename: '',
          qqAccount: '',
          score: '',
        },
        dataRule: {
          nicename: [{
            required: true,
            message: '玩家游戏昵称不能为空',
            trigger: 'blur'
          }],
          score: [{
            required: true,
            message: '玩家所得分数不能为空',
            trigger: 'blur'
          }],
          qqAccount: [{
            required: true,
            message: 'qq账号不能为空',
            trigger: 'blur'
          }],
        }
      }
    },
    methods: {
      init(gamePlayerId) {
        this.dataForm.gamePlayerId = gamePlayerId || 0;
        this.visible = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields();
          if (this.dataForm.gamePlayerId) {
            this.$http({
              url: this.$http.adornUrl(`/rank/rankbyredis/info/${this.dataForm.gamePlayerId}`),
              method: "get",
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                console.log(data)
                this.dataForm.score = data.info.score
                this.dataForm.nicename = data.info.nicename
                this.dataForm.qqAccount = data.info.qqAccount
              }
            });
          }
        });
      },
      // 表单提交
      dataFormSubmit() {
        this.loading = true;
        this.$refs["dataForm"].validate(valid => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/rank/rankbyredis/${!this.dataForm.gamePlayerId ? "save" : "update"}`),
              method: "post",
              data: this.$http.adornData(
                this.dataForm
              )
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.loading = false;
                    this.$emit("refreshDataList");
                  }
                });
              } else {
                this.$message.error(data.msg);
                this.loading = false;
              }
            });
          }
        });
      },
    }
  }

</script>
